package com.chenyue.cm.live.dao.provider;

import org.apache.ibatis.jdbc.SQL;

/**
 * @author libingxing
 */
public class LiveRecordExProvider {


    public String liveListCount(Integer liveState, Integer matchType, Integer state, int userId, String city, String province) {
        return new SQL() {
            {
                SELECT("count(0)");
                FROM("jd_live_record r");
                LEFT_OUTER_JOIN("jd_live l on r.live_id = l.id");
                LEFT_OUTER_JOIN("format_live_record lr on lr.id = r.id");
                LEFT_OUTER_JOIN("jd_match m on m.id = lr.match_id");
                WHERE("(m.state = 1 || isnull(m.id))");
                if (city != null) {
                    WHERE("( isnull(m.province_code) || m.city_code = #{city} || (isnull(m.city_code) && m.province_code = #{province}))");
//                    WHERE("( isnull(m.city_code) || m.city_code = #{city})");
                }
                if (matchType != null && matchType > 0) {
                    WHERE("m.type = #{matchType}");
                }
                if (liveState != null && liveState > -1) {
                    if (liveState == 0) {
//                        未开播
                        WHERE("isnull(r.start_time)");
                    }
                    if (liveState == 1) {
//                        正在播
                        WHERE("!isnull(r.start_time) and isnull(r.end_time)");
                    }
                    if (liveState == 2) {
//                        播完了
                        WHERE("!isnull(r.end_time)");
                    }
                } else {
                    WHERE("(isnull(r.start_time) || !isnull(r.end_time))");
                }
                if (state != null && state > 0) {
                    if (state == 1) {
                        WHERE("1 = (select 1 from jd_user_relation where source_user = #{userId} and relation_user = l.user_id and role = 1)");
                    }
                    if (state == 2) {
                        ORDER_BY("(select count(0) from jd_live_view_record where live_record_id = id)");
                    }
                    if (state == 3) {
                        ORDER_BY("r.start_time desc");
                    }
                }
            }
        } + "";
    }

    public String liveList(Integer liveState, Integer matchType, String convertPage, Integer state, int userId, String city, String province) {
        return new SQL() {
            {
                SELECT("r.id AS id");
                SELECT("r.match_id AS matchId");
                SELECT("r.live_id AS liveId");
                SELECT("r.arrange_id AS arrangeId");
                SELECT("r.title AS title");
                SELECT("r.live_logo AS liveLogo");
                SELECT("r.start_time AS startTime");
                SELECT("r.end_time AS endTime");
                SELECT("r.match_type AS matchType");
                SELECT("r.flv_pull_url AS flvPullUrl");
                SELECT("r.m3u8_pull_url AS m3u8PullUrl");
                SELECT("r.rtmp_pull_url AS rtmpPullUrl");
                SELECT("r.udp_pull_url AS udpPullUrl");
                SELECT("r.video_url AS videoUrl");
                SELECT("f.start_time as arrangeStartTime");
                SELECT("f.end_time as arrangeEndTime");
                SELECT("l.type as type");
                SELECT("(select name from jd_third_information where user_id = m.cu) as hostUnit");
                SELECT("(select user_name from jd_app_user where id = l.user_id) as anchorName");
                SELECT("(select head_img from jd_app_user where id = l.user_id) as anchorHeadImg");
                SELECT("(select count(0) from jd_live_view_record where live_record_id = r.id) as online_count");
                FROM("jd_live_record r");
                LEFT_OUTER_JOIN("jd_match_format f on r.arrange_id = f.id");
                LEFT_OUTER_JOIN("jd_match m on f.match_id = m.id");
                LEFT_OUTER_JOIN("jd_live l on r.live_id = l.id");
                WHERE("(m.state = 1 || isnull(m.id))");
                if (city != null) {
                    WHERE("(m.city_code = #{city} || isnull(m.province_code) || (isnull(m.city_code) && m.province_code = #{province}))");
//                    WHERE("( isnull(m.city_code) || m.city_code = #{city})");
                }
                if (liveState != null && liveState > -1) {
                    if (liveState == 0) {
//                        未开播
                        WHERE("isnull(r.start_time)");
                    }
                    if (liveState == 1) {
//                        正在播
                        WHERE("!isnull(r.start_time) and isnull(r.end_time)");
                    }
                    if (liveState == 2) {
//                        播完了
                        WHERE("!isnull(r.end_time)");
                    }
                } else {
                    WHERE("(isnull(r.start_time) || !isnull(r.end_time))");
                }
                if (matchType != null && matchType > 0) {
                    WHERE("m.type = #{matchType}");
                }
                if (state != null && state > 0) {
                    if (state == 1) {
                        WHERE("1 = (select 1 from jd_user_relation where source_user = #{userId} and relation_user = l.user_id)");
                    }
                    if (state == 2) {
                        ORDER_BY("(select count(0) from jd_live_view_record where live_record_id = r.id)");
                    }
                }
                ORDER_BY("r.start_time desc");
            }
        } + convertPage;
    }

}
